/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecDetalleItemException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecDetalleItemRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.service.IFeriaDecDetalleItemService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.FeriaDecDetalleItem;

/**
 * @author Usuario
 * 
 */
public class FeriaDecDetalleItemController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public FeriaDecDetalleItemController() {
		this.feriaDecDetalleItemListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda FeriaDecDetalleItem */
	public FeriaDecDetalleItem feriaDecDetalleItemSearch = new FeriaDecDetalleItem();

	/** Objeto contenedor del listado de FeriaDecDetalleItem luego de una consulta. */
	private DataModel feriaDecDetalleItemListModel;

	/** bean para el manejo del formulario de creacion/edicion. FeriaDecDetalleItem */
	private FeriaDecDetalleItem feriaDecDetalleItem = new FeriaDecDetalleItem();

	/** instancia de servicio feriaDecDetalleItemService */
	private IFeriaDecDetalleItemService feriaDecDetalleItemService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchFeriaDecDetalleItemByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getFeriaDecDetalleItemService().load(1, 10,
					this.feriaDecDetalleItemSearch);
			this.feriaDecDetalleItemListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecDetalleItemListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecDetalleItemListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}
	
	public String searchFeriaDecDetalleItemByAgno() {
		String msg = "";
		try {
			List valores = null;
			valores = getFeriaDecDetalleItemService().loadByAgno(1, 10,
					this.feriaDecDetalleItemSearch);
			this.feriaDecDetalleItemListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecDetalleItemListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecDetalleItemListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}
	
	public String searchFeriaDecDetalleItemByAgno(IFeriaDecDetalleItemService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.loadByAgno(1, 10,
					this.feriaDecDetalleItemSearch);
			this.feriaDecDetalleItemListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecDetalleItemListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecDetalleItemListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaDecDetalleItem.getId().longValue() == 0L) {
				feriaDecDetalleItem.setIdTipoItem(new Long(feriaDecDetalleItem.getIdTipoItemStr()));
				feriaDecDetalleItem.setOrden(new Long(feriaDecDetalleItem.getOrdenStr()));
				getFeriaDecDetalleItemService().insert(this.feriaDecDetalleItem);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (FeriaDecDetalleItemException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaDecDetalleItemRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- feriaDecDetalleItem " + msg, null, null);
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaDecDetalleItem.getId().longValue() != 0L) {
				feriaDecDetalleItem.setIdTipoItem(new Long(feriaDecDetalleItem.getIdTipoItemStr()));
				feriaDecDetalleItem.setOrden(new Long(feriaDecDetalleItem.getOrdenStr()));
				getFeriaDecDetalleItemService().update(this.feriaDecDetalleItem);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (FeriaDecDetalleItemException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaDecDetalleItemRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createFeriaDecDetalleItem() {
		feriaDecDetalleItem = new FeriaDecDetalleItem();
		feriaDecDetalleItem.setId(new Long(0L));
		feriaDecDetalleItem.setIdFeriaDecembrinaForm(feriaDecDetalleItemSearch.getIdFeriaDecembrinaForm());
		//feriaDecDetalleItem.setCodigo("");
		//feriaDecDetalleItem.setValor("");
		//feriaDecDetalleItem.setActivo("N");
		return AppConstants.FERIA_DEC_DETALLE_ITEM_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateFeriaDecDetalleItem() {
		Map params = getExternalContext().getRequestParameterMap();
		String feriaDecDetalleItemDataIndex = (String) params
				.get("feriaDecDetalleItemDataIndex");
		this.feriaDecDetalleItem = (FeriaDecDetalleItem) ((List) this.feriaDecDetalleItemListModel
				.getWrappedData())
				.get(Integer.parseInt(feriaDecDetalleItemDataIndex));
		return AppConstants.FERIA_DEC_DETALLE_ITEM_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelFeriaDecDetalleItem() {
		return AppConstants.FERIA_DEC_DETALLE_ITEM_SEARCH_VIEW;
	}

	public String cancelFeriaDecDetalleItemOpcion() {		
		return AppConstants.FERIA_DEC_DETALLE_ITEM_MENU_VIEW;
	}
	
	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public FeriaDecDetalleItem getFeriaDecDetalleItemSearch() {
		return feriaDecDetalleItemSearch;
	}

	/**
	 * 
	 * @param feriaDecDetalleItemSearch
	 */
	public void setferiaDecDetalleItemSearch(FeriaDecDetalleItem feriaDecDetalleItemSearch) {
		this.feriaDecDetalleItemSearch = feriaDecDetalleItemSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getferiaDecDetalleItemListModel() {
		return feriaDecDetalleItemListModel;
	}

	/**
	 * 
	 * @param feriaDecDetalleItemListModel
	 */
	public void setferiaDecDetalleItemListModel(DataModel feriaDecDetalleItemListModel) {
		this.feriaDecDetalleItemListModel = feriaDecDetalleItemListModel;
	}

	/**
	 * 
	 * @return
	 */
	public FeriaDecDetalleItem getFeriaDecDetalleItem() {
		return feriaDecDetalleItem;
	}

	/**
	 * 
	 * @param feriaDecDetalleItem
	 */
	public void setferiaDecDetalleItem(FeriaDecDetalleItem feriaDecDetalleItem) {
		this.feriaDecDetalleItem = feriaDecDetalleItem;
	}

	/**
	 * obtencion del servicio feriaDecDetalleItemService
	 * 
	 * @return
	 */
	public IFeriaDecDetalleItemService getFeriaDecDetalleItemService() {
		return feriaDecDetalleItemService;
	}

	/**
	 * establecimiento del servicio feriaDecDetalleItemService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param feriaDecDetalleItemService
	 */
	public void setFeriaDecDetalleItemService(
			IFeriaDecDetalleItemService feriaDecDetalleItemService) {
		this.feriaDecDetalleItemService = feriaDecDetalleItemService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
